/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.bo;

import java.util.ArrayList;
import java.util.HashMap;
import uit.tkorg.coauthorgraph.dbaccess.AuthorPaperMapper;
import uit.tkorg.coauthorgraph.dbaccess.JournalMapper;
import uit.tkorg.coauthorgraph.dto.JournalDTO;

/**
 *
 * @author Huynh Ngoc Tin
 */
public class JournalBO {

     /**
     * check if the specified journal exist in the DB
     * @param journalDTO
     * @return
     * @throws Exception
     */
    public boolean isJournalExisted(JournalDTO journalDTO) throws Exception {
        JournalMapper mapper = null;
        try {
            mapper = new JournalMapper();
            return mapper.isJournalExisted(journalDTO);
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            mapper.closeConnection();
        }
    }

    /**
     * insert a new journal into the DB
     * @param journalDTO
     * @return
     * @throws Exception
     */
    public int insertNewJournal(JournalDTO journalDTO) throws Exception {
        JournalMapper mapper = null;
        try {
            mapper = new JournalMapper();
            return mapper.insertNewJournal(journalDTO);
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            mapper.closeConnection();
        }
    }

    /**
     * get journalID from journalName and year
     * @param journalName
     * @param year
     * @return
     * @throws Exception
     */
    public int getJournalID(String journalName, int year) throws Exception {
        JournalMapper mapper = null;
        try {
            mapper = new JournalMapper();
            return mapper.getJournalID(journalName, year);
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            mapper.closeConnection();
        }
    }

    /**
     * Metrics #2: calculateTotalJournalPercentage
     * @param authorIDList
     * @return
     * @throws Exception
     */
    public HashMap calculateTotalJournalPercentage(ArrayList authorIDList) throws Exception {
        HashMap jourPaperPercentHashMap = new HashMap();
        JournalMapper jourMapper = new JournalMapper();
        AuthorPaperMapper authorPaperMapper = new AuthorPaperMapper();
        try {
            ArrayList jourNameList = jourMapper.getJournalNameListForAuthorList(authorIDList);
            for (int i=0; i<jourNameList.size(); i++) {
                String jourName = (String) jourNameList.get(i);
                double percentSum = 0;
                for (int j=0; j<authorIDList.size(); j++) {
                    int curAuthorID = Integer.parseInt((String) authorIDList.get(j)) ;
                    double totalPaperOfAuthor = authorPaperMapper.countTotalPaperOfAnAuthor(curAuthorID);
                    double totalPaperOfAuthorInJour = authorPaperMapper.countTotalPaperInJourOfAnAuthor(curAuthorID, jourName);
                    double percentPaperOfAuthorInConf = totalPaperOfAuthorInJour/totalPaperOfAuthor;
                    percentSum += percentPaperOfAuthorInConf;
                }
                jourPaperPercentHashMap.put(jourName, new Double(percentSum));
            }
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            jourMapper.closeConnection();
            authorPaperMapper.closeConnection();
        }

        return jourPaperPercentHashMap;
    }

    /**
     * Metrics #3: calculateTotalJournalPercentWithCoAuthor
     * @param authorVertexIDList
     * @param initialAuthorIDList
     * @return
     * @throws Exception
     */
    public HashMap calculateTotalJournalPercentWithCoAuthor(ArrayList authorVertexIDList, ArrayList initialAuthorIDList) throws Exception {
        JournalMapper jourMapper = new JournalMapper();
        AuthorPaperMapper authorPaperMapper = new AuthorPaperMapper();

        HashMap totalJourPercentWithCoAuthorHashMap = new HashMap();
        try {
            ArrayList jourNameList = jourMapper.getJournalNameListForAuthorList(authorVertexIDList);

            // Loop through all of Journals in the CoAuthor-Network
            for (int i=0; i<jourNameList.size(); i++) {
                String jourName = (String) jourNameList.get(i);
                double sumJourPercentWithCoAuthor = 0;
                for (int j=0; j<initialAuthorIDList.size(); j++) {
                    double curTotalJourPercentWithCoAuthor = 0;
                    int curInitialAuthorID = ((Integer) initialAuthorIDList.get(j)).intValue();

                    // get % papers of curInitialAuthorID in the current journal
                    double totalPaperOfCurInitialAuthorInJour = authorPaperMapper.countTotalPaperInJourOfAnAuthor(curInitialAuthorID, jourName);
                    double totalPaperOfCurInitialAuthor = authorPaperMapper.countTotalPaperOfAnAuthor(curInitialAuthorID);
                    double percentPaperOfCurInitialAuthorInJour = totalPaperOfCurInitialAuthorInJour/totalPaperOfCurInitialAuthor;

                    // get the Co-AuthorList of curInitialAuthorID
                    ArrayList coAuthorIDList = authorPaperMapper.getCoAuthorIDList(curInitialAuthorID);

                    // Loop through all co-authors of curInitialAuthorID
                    for (int k=0; k<coAuthorIDList.size(); k++) {
                        int coAuthorID = ((Integer) coAuthorIDList.get(k)).intValue();
                        // get % papers of coAuthorID in the current conference
                        double totalPaperOfAnCoAuthorInJour = authorPaperMapper.countTotalPaperInJourOfAnAuthor(coAuthorID, jourName);
                        double totalPaperOfAnCoAuhor = authorPaperMapper.countTotalPaperOfAnAuthor(coAuthorID);
                        double percentPaperOfCoAuthorInJour = totalPaperOfAnCoAuthorInJour/totalPaperOfAnCoAuhor;
                        // count the number of papers which coAuthorID & curInitialAuthorID is the co-author
                        double numberOfPapersCoAuthor = authorPaperMapper.countTotalPaperOfTwoAuthorIsCoAuthor(curInitialAuthorID, coAuthorID);

                        curTotalJourPercentWithCoAuthor += (percentPaperOfCurInitialAuthorInJour + percentPaperOfCoAuthorInJour)
                                                                *numberOfPapersCoAuthor;
                    } // end of while (loop all of co-authors)

                    sumJourPercentWithCoAuthor += curTotalJourPercentWithCoAuthor;
                }

                totalJourPercentWithCoAuthorHashMap.put(jourName, new Double(sumJourPercentWithCoAuthor));
            } // end of while(...)
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            jourMapper.closeConnection();
            authorPaperMapper.closeConnection();
        }

        return totalJourPercentWithCoAuthorHashMap;
    }
}
